package com.project.movie.controller.type;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieItem;
import com.project.movie.domain.MovieType;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 电影类型controller
 * @date 2025/02/06 09:02
 */
@Controller
@ResponseBody
@RequestMapping("type")
public class MovieTypeController {

    @Autowired
    private MovieTypeService movieTypeService;
    @Autowired
    private MovieItemService movieItemService;

    /** 分页获取电影类型 */
    @PostMapping("getMovieTypePage")
    public Result getMovieTypePage(@RequestBody MovieType movieType) {
        Page<MovieType> page = new Page<>(movieType.getPageNumber(),movieType.getPageSize());
        QueryWrapper<MovieType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieType.getName()),MovieType::getName,movieType.getName());
        Page<MovieType> movieTypePage = movieTypeService.page(page, queryWrapper);
        return Result.success(movieTypePage);
    }

    @GetMapping("getMovieTypeList")
    public Result getMovieTypeList() {
        List<MovieType> list = movieTypeService.list();
        return Result.success(list);
    }

    /** 根据id获取电影类型 */
    @GetMapping("getMovieTypeById")
    public Result getMovieTypeById(@RequestParam("id")String id) {
        MovieType movieType = movieTypeService.getById(id);
        return Result.success(movieType);
    }

    /** 保存电影类型 */
    @PostMapping("saveMovieType")
    public Result saveMovieType(@RequestBody MovieType movieType) {
        boolean save = movieTypeService.save(movieType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑电影类型 */
    @PostMapping("editMovieType")
    public Result editMovieType(@RequestBody MovieType movieType) {
        boolean save = movieTypeService.updateById(movieType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除电影类型 */
    @GetMapping("removeMovieType")
    public Result removeMovieType(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                MovieType movieType = movieTypeService.getById(id);
                QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().like(MovieItem::getType,movieType.getName());
                int count = movieItemService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("分类下存在电影无法删除");
                }
                movieTypeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("电影类型id不能为空！");
        }
    }

}
